#! /usr/bin/python
# -*- coding: cp1250 -*-
#sys.path.append(r'C:\XXX\YYY')

import glob, re, string, sys, os
import pepe.p_txt.bigtxt as bt

def main(inp_file_mask, key_col_no, value_col_no, col_delimiter, skip_header='noskip_header'):
    ''' '''
    key_col_no = int(key_col_no)
    value_col_no = int(value_col_no)
    output = open('sum-per-key.log', 'w')
    accd = {}

    for inpfile in glob.glob(inp_file_mask):
        lino = 0
        
        for line in bt.lines(inpfile):
            lino += 1
            if lino == 1 and skip_header == 'skip_header':
                pass
            else:
                line = re.sub('\n', '', line)
                cols = string.split(line, col_delimiter)

                if (len(cols)-1) > max([key_col_no, value_col_no]):

                    if accd.get(cols[key_col_no]):
                        accd[cols[key_col_no]] += float(string.strip(cols[value_col_no]))
                    else:
                        accd[cols[key_col_no]] = float(string.strip(cols[value_col_no]))
            
    for k in sorted(accd.keys()):
        output.write(k+'|'+str(accd[k])+'\n')
    output.close()
    return 1

HELP = """
Purpose:    Sums per key corresponding values.
Usage:      sum-per-key.py  <file mask> <key column no>
                            <value column no> <column delimiter>
                            <skip_header>
Example:    sum-per-key.py *.csv 1 2 "^" skip_header
"""

if __name__ == '__main__':
    if len(sys.argv) == 6:
        main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])
    elif len(sys.argv) == 5:
        main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
    else:
        print HELP
    #main('*.txt', 1, 2, "^")
